package com.realife.ysuper.controller;

import com.realife.common.utils.R;
import com.realife.ysuper.entity.UserEntity;
import com.realife.ysuper.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 用户表
 *
 * @author Albert
 * @email 15962463559@163.com
 * @date 2020-06-05 10:04:15
 */
@Slf4j
@RestController
@RequestMapping("/ysuper/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("ysuper:user:info")
    public R info(@PathVariable("id") Long id) {
        UserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ysuper:user:save")
    public R save(@RequestBody UserEntity user) {
        userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("ysuper:user:update")
    public R update(@RequestBody UserEntity user) {
        userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ysuper:user:delete")
    public R delete(@RequestBody Long[] ids) {
        userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/login")
    public R login(@RequestBody Map map) {

        System.out.println(map);
        return R.ok().put("code", 20000).put("token", "admin");
    }


    @RequestMapping("/info")
    public R info() {
        return R.ok().put("code", 20000).put("roles", "[admin]").put("name", "admin").put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
    }
}
